package com.hu.zhcc.shiro.service.junM.Impl;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.common.entity.PageRequest;
import com.hu.zhcc.shiro.dao.junM.*;
import com.hu.zhcc.shiro.dao.lgx.THealthmgrMapper;
import com.hu.zhcc.shiro.dao.lgx.TMemberMapper;
import com.hu.zhcc.shiro.dao.lgx.TMemberMedicalhistoryMapper;
import com.hu.zhcc.shiro.dao.xsf.TDiseaseLibraryMapper;
import com.hu.zhcc.shiro.entity.junM.dto.TInterposeClassifyDTO;
import com.hu.zhcc.shiro.entity.junM.pojo.*;
import com.hu.zhcc.shiro.entity.lgx.THealthmgr;
import com.hu.zhcc.shiro.entity.lgx.TMember;
import com.hu.zhcc.shiro.entity.lgx.TMemberMedicalhistory;
import com.hu.zhcc.shiro.entity.xsf.pojo.TDiseaseLibrary;
import com.hu.zhcc.shiro.service.junM.InterposeSchemeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class InterposeSchemeServiceImpl implements InterposeSchemeService {

    @Autowired
    private TMemberMapper tMemberMapper;
    @Autowired
    private TInterposeClassifyMapper tInterposeClassifyMapper;
    @Autowired
    private TMemberMedicalhistoryMapper tMemberMedicalhistoryMapper;
    @Autowired
    private TDiseaseLibraryMapper tDiseaseLibraryMapper;
    @Autowired
    private TInterposeSchemeMapper tInterposeSchemeMapper;
    @Autowired
    private TInterposeFoodMapper tInterposeFoodMapper;
    @Autowired
    private TInterposeSprotMapper tInterposeSprotMapper;
    @Autowired
    private TInterposePlainMapper tInterposePlainMapper;
    @Autowired
    private THealthmgrMapper tHealthmgrMapper;

    @Override
    public Page<TMember> queryMemberByStatusAll(Map<String, Object> paremeters, int offset, int limit) {

        int total = this.tMemberMapper.countMemberByStatus(paremeters);
        List<TMember> dtoList = null;
        if(total>0){
            PageRequest pageRequest = new PageRequest(offset, limit, paremeters, null);
            List<TMember> list = this.tMemberMapper.queryMemberByStatusAll(pageRequest);
            dtoList = new ArrayList<TMember>(list.size());
            for (TMember tInterposeClassify : list) {
                TMember dto = new TMember();
                BeanUtils.copyProperties(tInterposeClassify, dto);
                dtoList.add(dto);
            }
        }
        return new Page<TMember>(total,dtoList);
    }

    @Override
    public TMember getId(Integer id) {
        //病例史
        TMemberMedicalhistory medicalhistorys = this.tMemberMedicalhistoryMapper.selectByMemberId(id);
        //疾病库
        List<TDiseaseLibrary> tDiseaseLibraries = tDiseaseLibraryMapper.queryDiseaseLibraryByMemberId(id);
        TMember tMember = tMemberMapper.selectByPrimaryKey(id);
        tMember.setTMemberMedicalhistory(medicalhistorys);
        tMember.setTDiseaseLibrarys(tDiseaseLibraries);
        return tMember;
    }

    @Override
    public void addInterposeScheme(int id, TInterposeScheme scheme, TInterposeFood food, TInterposeSprot sprot, TInterposePlain plain) {

        scheme.setTMemberId(id);
        food.setTMemberId(id);
        sprot.setTMemberId(id);
        plain.setTMemberId(id);

        TInterposeClassify tInterposeClassify = tInterposeClassifyMapper.selectByMemberId(id);
        tInterposeClassify.setStatus(2);
        tInterposeClassifyMapper.updateByPrimaryKey(tInterposeClassify);

        this.tInterposeSchemeMapper.insertSelective(scheme);
        this.tInterposeFoodMapper.insertSelective(food);
        this.tInterposeSprotMapper.insertSelective(sprot);
        this.tInterposePlainMapper.insertSelective(plain);

    }

    @Override
    public List<THealthmgr> queryHealthmgrAll() {
        System.out.println(tHealthmgrMapper.queryHealthmgrAll());
        return this.tHealthmgrMapper.queryHealthmgrAll();
    }
}
